<?php
//自动生成,请根据需要修改
namespace app\mqtt\controller;
use app\common\controller\Base;
use think\facade\Request;
use think\Db;
/**
* @title 聊天人员接口
* @description 聊天人员说明
* @group mqtt
*/
class ChatMember extends Base{

    /**
     * @title 插入记录
     * @description 插入聊天人员数据
     * @author 开发者（袁丽蓉）
     * @url /mqtt/chat_member/add
     * @method post
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:title type:Int require:1 default:0 other: desc:账号id
     * @param name:account_id type:Int require:1 default:0 other: desc:创建账号id
     * @param name:add_time type:Int require:1 default:0 other: desc:加入时间
     * @param name:status type:TinyInt require:1 default:0 other: desc:状态
     * @param name:is_deleted type:TinyInt require:1 default:0 other: desc:已删除
     * @param name:can_send type:TinyInt require:1 default:0 other: desc:可发送
     * @param name:in_accept type:TinyInt require:1 default:0 other: desc:可接收
     * @param name:info type:VarChar require:1 default:0 other: desc:简介
     * @param name:chat_type type:VarChar require:1 default:0 other: desc:窗口类型
     * @param name:nickname type:VarChar require:1 default:0 other: desc:昵称
     * @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
     *
     *
     * @return title:账号id
     * @return account_id:创建账号id
     * @return add_time:加入时间
     * @return status:状态
     * @return is_deleted:已删除
     * @return can_send:可发送
     * @return in_accept:可接收
     * @return info:简介
     * @return chat_type:窗口类型
     * @return nickname:昵称
     * @return chat_id:窗口id
     *
     */
    public function add (){
        //模块 - 输入数据
        $input = input();

        //模块 - 验证器
        $chat_memberValidate = new \app\mqtt\validate\ChatMember;
        $validate = $chat_memberValidate->scene('except_id')->check($input);
        if (!$validate) {
            $msg['msg'] = '验证失败';
            $msg['validate'] = $chat_memberValidate->getError();
            return $this->errorJson($msg);
        }

        // ---	模块 - 新增  ---
        //插入的数据
        $insert_data = [
            'title'=>$input['title'],
            'account_id'=>$input['account_id'],
            'add_time'=>$input['add_time'],
            'status'=>$input['status'],
            'is_deleted'=>$input['is_deleted'],
            'can_send'=>$input['can_send'],
            'in_accept'=>$input['in_accept'],
            'info'=>$input['info'],
            'chat_type'=>$input['chat_type'],
            'nickname'=>$input['nickname'],
            'chat_id'=>$input['chat_id'],
        ];
        //添加数据
        $insert_res = Db::name('chat_member')->data($insert_data)->insert();
        if(!$insert_res){
            $msg['msg'] = '插入数据失败！';
            return $this->errorJson($msg);
        }
        $msg['res'] = $insert_res;


        //接口输出json数据
        $msg['msg'] = '操作成功！';
        return $this->successJson($msg);
    }
    /**
     * @title 获取列表数据（paginate）
     * @description 获取聊天人员的列表数据（分页）
     * @author 开发者（袁丽蓉）
     * @url /mqtt/chat_member/get_list
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:Int require:1 default:0 other: desc:id
     * @param name:title type:Int require:1 default:0 other: desc:账号id
     * @param name:account_id type:Int require:1 default:0 other: desc:创建账号id
     * @param name:add_time type:Int require:1 default:0 other: desc:加入时间
     * @param name:status type:TinyInt require:1 default:0 other: desc:状态
     * @param name:is_deleted type:TinyInt require:1 default:0 other: desc:已删除
     * @param name:can_send type:TinyInt require:1 default:0 other: desc:可发送
     * @param name:in_accept type:TinyInt require:1 default:0 other: desc:可接收
     * @param name:info type:VarChar require:1 default:0 other: desc:简介
     * @param name:chat_type type:VarChar require:1 default:0 other: desc:窗口类型
     * @param name:nickname type:VarChar require:1 default:0 other: desc:昵称
     * @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
     *
     * @param name:page type:Int require:0 default:0 other: desc:当前页
     * @param name:pagesize type:Int require:0 default:0 other: desc:每页数量
     *
     * @return id:id
     * @return title:账号id
     * @return account_id:创建账号id
     * @return add_time:加入时间
     * @return status:状态
     * @return is_deleted:已删除
     * @return can_send:可发送
     * @return in_accept:可接收
     * @return info:简介
     * @return chat_type:窗口类型
     * @return nickname:昵称
     * @return chat_id:窗口id
     *
     */
    public function get_list (){
        //模块 - 输入数据
        $input = input();


        // ---	模块 - 查询  ---
        //查询条件
        $temp_map = [
            ['id','=',$input['id']],
            ['title','=',$input['title']],
            ['account_id','=',$input['account_id']],
            ['add_time','=',$input['add_time']],
            ['status','=',$input['status']],
            ['is_deleted','=',$input['is_deleted']],
            ['can_send','=',$input['can_send']],
            ['in_accept','=',$input['in_accept']],
            ['info','like','%'.$input['info'].'%'],
            ['chat_type','like','%'.$input['chat_type'].'%'],
            ['nickname','like','%'.$input['nickname'].'%'],
            ['chat_id','=',$input['chat_id']],
        ];
        //去除为空的条件
        $paginate_map = [];
        foreach ($temp_map as $k=>$v){
            if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
                continue;
            }
            array_push($paginate_map,$v);
        }

        //获取数据
        $select_res = Db::name('chat_member')->where($paginate_map)->paginate();

        // paginate 返回数据
        $msg['list'] = $select_res;


        //接口输出json数据
        $msg['msg'] = '操作成功！';
        return $this->successJson($msg);
    }
    /**
     * @title 获取列表数据（select）
     * @description 获取聊天人员的列表数据（无分页）
     * @author 开发者（袁丽蓉）
     * @url /mqtt/chat_member/get_select
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:Int require:1 default:0 other: desc:id
     * @param name:title type:Int require:1 default:0 other: desc:账号id
     * @param name:account_id type:Int require:1 default:0 other: desc:创建账号id
     * @param name:add_time type:Int require:1 default:0 other: desc:加入时间
     * @param name:status type:TinyInt require:1 default:0 other: desc:状态
     * @param name:is_deleted type:TinyInt require:1 default:0 other: desc:已删除
     * @param name:can_send type:TinyInt require:1 default:0 other: desc:可发送
     * @param name:in_accept type:TinyInt require:1 default:0 other: desc:可接收
     * @param name:info type:VarChar require:1 default:0 other: desc:简介
     * @param name:chat_type type:VarChar require:1 default:0 other: desc:窗口类型
     * @param name:nickname type:VarChar require:1 default:0 other: desc:昵称
     * @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
     *
     *
     * @return id:id
     * @return title:账号id
     * @return account_id:创建账号id
     * @return add_time:加入时间
     * @return status:状态
     * @return is_deleted:已删除
     * @return can_send:可发送
     * @return in_accept:可接收
     * @return info:简介
     * @return chat_type:窗口类型
     * @return nickname:昵称
     * @return chat_id:窗口id
     *
     */
    public function get_select (){
        //模块 - 输入数据
        $input = input();

        // ---	模块 - 查询  ---
        //查询条件
        $temp_map = [
            ['id','=',$input['id']],
            ['title','=',$input['title']],
            ['account_id','=',$input['account_id']],
            ['add_time','=',$input['add_time']],
            ['status','=',$input['status']],
            ['is_deleted','=',$input['is_deleted']],
            ['can_send','=',$input['can_send']],
            ['in_accept','=',$input['in_accept']],
            ['info','like','%'.$input['info'].'%'],
            ['chat_type','like','%'.$input['chat_type'].'%'],
            ['nickname','like','%'.$input['nickname'].'%'],
            ['chat_id','=',$input['chat_id']],
        ];
        //去除为空的条件
        $select_map = [];
        foreach ($temp_map as $k=>$v){
            if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
                continue;
            }
            array_push($select_map,$v);
        }

        //获取数据
        $select_res = Db::name('chat_member')->where($select_map)->select();

        // select 返回数据
        $msg['list'] = $select_res;


        //接口输出json数据
        $msg['msg'] = '操作成功！';
        return $this->successJson($msg);
    }
	/**
	* @title 获取单条数据
	* @description 根据ID获取聊天人员的指定记录
	* @author 开发者（袁丽蓉）
	* @url /mqtt/chat_member/info
	* @method *
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	*
	*
	* @return id:id
	*
	*/
	public function info (){
		//模块 - 输入数据
		$input['id'] = Request::param('id/d');


		//模块 - 验证器
		$chat_memberValidate = new \app\mqtt\validate\ChatMember;
		$validate = $chat_memberValidate->scene('only_id')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $chat_memberValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 查询  ---
		//查询条件
		$temp_map = [
			['id','=',$input['id']],
		];
		//去除为空的条件
		$find_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2]===null || $v[2]=='' || $v[2]=='%%'){
				continue;
			}
			array_push($find_map,$v);
		}

		//获取数据
		$select_res = Db::name('chat_member')->where($find_map)->find();

		// find 返回数据
		$msg['data'] = $select_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 更新记录
	* @description 根据ID更新聊天人员指定记录的数据
	* @author 开发者（袁丽蓉）
	* @url /mqtt/chat_member/edit
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:title type:Int require:1 default:0 other: desc:账号id
	* @param name:account_id type:Int require:1 default:0 other: desc:创建账号id
	* @param name:add_time type:Int require:1 default:0 other: desc:加入时间
	* @param name:status type:TinyInt require:1 default:0 other: desc:状态
	* @param name:is_deleted type:TinyInt require:1 default:0 other: desc:已删除
	* @param name:can_send type:TinyInt require:1 default:0 other: desc:可发送
	* @param name:in_accept type:TinyInt require:1 default:0 other: desc:可接收
	* @param name:info type:VarChar require:1 default:0 other: desc:简介
	* @param name:chat_type type:VarChar require:1 default:0 other: desc:窗口类型
	* @param name:nickname type:VarChar require:1 default:0 other: desc:昵称
	* @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
	*
	*
	* @return id:id
	* @return title:账号id
	* @return account_id:创建账号id
	* @return add_time:加入时间
	* @return status:状态
	* @return is_deleted:已删除
	* @return can_send:可发送
	* @return in_accept:可接收
	* @return info:简介
	* @return chat_type:窗口类型
	* @return nickname:昵称
	* @return chat_id:窗口id
	*
	*/
	public function edit (){
		//模块 - 输入数据
        $input = input();

		//模块 - 验证器
		$chat_memberValidate = new \app\mqtt\validate\ChatMember;
		$validate = $chat_memberValidate->scene('all_field')->check($input);
		if (!$validate) {
			$msg['msg'] = '验证失败';
			$msg['validate'] = $chat_memberValidate->getError();
			return $this->errorJson($msg);
		}

		// ---	模块 - 修改  ---
		//更新的条件
		$update_date = [
		'id'=>$input['id'],
		];
		//更新的数据
		$update_map = [
			'title'=>$input['title'],
			'account_id'=>$input['account_id'],
			'add_time'=>$input['add_time'],
			'status'=>$input['status'],
			'is_deleted'=>$input['is_deleted'],
			'can_send'=>$input['can_send'],
			'in_accept'=>$input['in_accept'],
			'info'=>$input['info'],
			'chat_type'=>$input['chat_type'],
			'nickname'=>$input['nickname'],
			'chat_id'=>$input['chat_id'],
		];
		//获取数据
		$update_res = Db::name('chat_member')->where($update_date)->update($update_map);
		if(!$update_res){
			$msg['msg'] = '更新据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $update_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
	/**
	* @title 按条件删除记录
	* @description 按条件删除聊天人员的数据
	* @author 开发者（袁丽蓉）
	* @url /mqtt/chat_member/delete
	* @method post
	*
	* @param name:token require:1 default: desc:token
	*
	* @param name:id type:Int require:1 default:0 other: desc:id
	* @param name:title type:Int require:1 default:0 other: desc:账号id
	* @param name:account_id type:Int require:1 default:0 other: desc:创建账号id
	* @param name:add_time type:Int require:1 default:0 other: desc:加入时间
	* @param name:status type:TinyInt require:1 default:0 other: desc:状态
	* @param name:is_deleted type:TinyInt require:1 default:0 other: desc:已删除
	* @param name:can_send type:TinyInt require:1 default:0 other: desc:可发送
	* @param name:in_accept type:TinyInt require:1 default:0 other: desc:可接收
	* @param name:info type:VarChar require:1 default:0 other: desc:简介
	* @param name:chat_type type:VarChar require:1 default:0 other: desc:窗口类型
	* @param name:nickname type:VarChar require:1 default:0 other: desc:昵称
	* @param name:chat_id type:Int require:1 default:0 other: desc:窗口id
	*
	*
	* @return id:id
	* @return title:账号id
	* @return account_id:创建账号id
	* @return add_time:加入时间
	* @return status:状态
	* @return is_deleted:已删除
	* @return can_send:可发送
	* @return in_accept:可接收
	* @return info:简介
	* @return chat_type:窗口类型
	* @return nickname:昵称
	* @return chat_id:窗口id
	*
	*/
	public function delete (){
		//模块 - 输入数据
        $input = input();
		

		// ---  模块 - 删除  ---
		//删除的条件
		$temp_map = [
			['id','=',$input['id']],
			['title','=',$input['title']],
			['account_id','=',$input['account_id']],
			['add_time','=',$input['add_time']],
			['status','=',$input['status']],
			['is_deleted','=',$input['is_deleted']],
			['can_send','=',$input['can_send']],
			['in_accept','=',$input['in_accept']],
			['info','=',$input['info']],
			['chat_type','=',$input['chat_type']],
			['nickname','=',$input['nickname']],
			['chat_id','=',$input['chat_id']],
		];
		$delete_map = [];
		foreach ($temp_map as $k=>$v){
			if ($v[2] == null || $v[2] == ''){
				continue;
			}
			array_push($delete_map,$v);
		}
		if(empty($delete_map)){
			$msg['msg'] = '删除条件不能为空';
			return $this->errorJson($msg);
		}

		//删除数据
		$delete_res = Db::name('chat_member')->where($delete_map)->delete();
		if(!$delete_res){
			$msg['msg'] = '删除数据失败！';
			return $this->errorJson($msg);
		}
		$msg['res'] = $delete_res;


		//接口输出json数据
		$msg['msg'] = '操作成功！';
		return $this->successJson($msg);
	}
    //TODO 同步信息
    /**
     * @title 同步信息
     * @description 同步信息
     * @author 开发者（）
     * @url /mqtt/chat_member/syn_service
     * @method *
     *
     * @param name:service_account_id type:Int require:1 default:0 other: desc:客服账号id
     * @param name:service_account_type type:Int require:1 default:0 other: desc:客服账号类型
     *
     * @return status:状态
     * @return msg:msg
     * @return data:data
     */
    public function syn_service ()
    {
        //模块 - 输入数据
        $input = input();
        //获取商城管理员
        $_user=get_user($input['service_account_id'], $type = "m");
        $user=Db::name('chat_member')->where('title',$input['service_account_id'])->find();
        if (!empty($_user['status'])) {
            $params['id']=$_user['account_id'];
            $params['nickname'] = $_user['nickname'];
            $params['phone'] = $_user['phone'];
            $params['head_img'] = $_user['logo'];
            $params['status'] = $_user['status'];
            if(empty($user['title'])){
                //插入的数据
                $insert_data = [
                    'nickname' => $params['nickname'],
                    'title' => $params['id'],
                    'phone' => $params['phone'],
                    'head_img' => $params['head_img'],
                    'status' => $params['status'],
                ];
                //添加数据
                $insert_res = Db::name('chat_member')->data($insert_data)->insert();
                if (!$insert_res) {
                    $msg['msg'] = '同步数据失败！';
                    return $this->errorJson($msg);
                }
                $msg['res'] = $insert_res;
                //接口输出json数据
                $msg['msg'] = '同步完成';
                return $this->successJson($msg);
            }
            //同步的条件
            $update_date = [
                'title' => $input['service_account_id'],
            ];
            //同步的数据
            $update_map = [
                'nickname' => $params['nickname'],
                'title' => $params['id'],
                'phone' => $params['phone'],
                'head_img' => $params['head_img'],
                'status' => $params['status'],
            ];
            $update_res = Db::name('chat_member')->where($update_date)->update($update_map);
            if (!$update_res) {
                $msg['msg'] = '已是最新数据！';
                return $this->errorJson($msg);
            }
            $msg['res'] = $update_res;
            //接口输出json数据
            $msg['msg'] = '同步成功！';
            return $this->successJson($msg);
        } else {
                $update_date = [
                    'title' => $input['service_account_id'],
                ];
                $update_res = Db::name('chat_member')->where($update_date)->update([
                    'is_deleted' => '1',
                    'is_accept' => '0',
                ]);
                if (!$update_res) {
                    $msg['msg'] = '最新数据！';
                    return $this->errorJson($msg);
                }
                $msg['res'] = $update_res;
                $msg['msg'] = '操作成功';
                return $this->successJson($msg);
            }
        }
}
